import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;


public class ConfigFileReader {
	

	
	public String filename = null;
	private String splitCharacter = ":";
	private String commentCharacter = "#";
	public String[][] servers;
	public String[] rootserver;
	public String[][] otherservers;
	
	public ConfigFileReader()
	{
		this.filename = "config.ini";
	}
	
	public ConfigFileReader(String filename)
	{
		this.filename = filename;
	}
	
	public void ParseFile() throws FileNotFoundException, IOException, NumberFormatException
	{
		FileReader inFile = null;
    	try{
    		inFile = new FileReader (filename); //open config file
    	}
    	catch (FileNotFoundException e) {
    		throw e;
    	}
		BufferedReader in = new BufferedReader (inFile); //open config reader
		
		//parse config
		String line = null;
		String[] server = null;
		Queue<String[]> serversQueue = new LinkedList<String[]>();
		while(in.ready())
		{
			line = in.readLine().trim();
			
			if(line.length() > 0 && !line.startsWith(commentCharacter) )
			{
				line = line.split(commentCharacter)[0].trim();
				server = extract1dNodeArray(line);
				if(server != null)
					serversQueue.add(server);
			}
		}
		rootserver = null;
		servers = new String[serversQueue.size()][];
		
		int serversindex = 0;
		int otherserversindex = 0;
		int rootserverindex = -1;
		while(serversQueue.size() > 0)
		{
			server = serversQueue.remove();
			servers[serversindex] = server;
			if(server.length > Helpers.PARENTMSD && server[Helpers.PARENTMSD].equalsIgnoreCase("root"))
			{
				rootserver = server;
				rootserverindex = serversindex;
			}
			serversindex++;
		}
		if(rootserver != null) //remove the blank entry at the end
		{
			otherservers = new String[servers.length-1][];
			int i2 = 0;
			for(int i1=0; i1 < servers.length; i1++)
			{
				if(i1 != rootserverindex)
				{
					otherservers[i2] = servers[i1];
					i2++;
				}
			}
		}
		else
			otherservers = servers;
		in.close();
		inFile.close();
	}
	

	private String[] extract1dNodeArray(String line)
	{
		String[] nodeString = line.split("\t");
		Queue<String> nodeValsQueue =new LinkedList<String>();
		for(String val : nodeString)
		{
			val = val.trim();
			if(val.length() > 0)
			{
				nodeValsQueue.add(val);
			}
		}

		nodeString = new String[nodeValsQueue.size()];
		int index = 0;
		while(nodeValsQueue.size() > 0)
		{
			nodeString[index] = nodeValsQueue.remove();
			index++;
		}
		return nodeString;
	}

	private String extractName(String line)
	{
		String name = line.split(splitCharacter)[0];
		name = name.trim();
		return name;
	}
	
	private String extractValue(String line)
	{
		String value = "";
		if(line.contains(splitCharacter))
		{
			if(line.contains(commentCharacter))
				if(line.indexOf(splitCharacter) > line.indexOf(commentCharacter))
					return "";
				else
					value = line.split(splitCharacter)[1].split(commentCharacter)[0];
			else
				value = line.split(splitCharacter)[1];
			if(value.contains(commentCharacter))
				value = value.split(commentCharacter)[0];
		}
		value = value.trim();
		return value;
	}
}
